//
//  HouseDynamicViewController.m
//  sellHouse
//
//  Created by 李 佳伟 on 2018/11/20.
//  Copyright © 2018年 李 佳伟. All rights reserved.
//

#import "HouseDynamicViewController.h"

#import "HouseListViewController.h"
#import "HouseTakeViewController.h"
#import <WHC_AutoLayoutKit/UITableViewCell+WHC_AutoHeightForCell.h>
#import "JTCSM.h"
#import "ShowImageView.h"
#import "houseInfoTableViewCell.h"
#import "housekeeperTableViewCell.h"
#import "housedynamicTableViewCell.h"
#import "housecountTableViewCell.h"
#import "HouserInfoModel.h"
#import "HouseZhexianModel.h"
#import "UILabel+Custom.h"


static CGFloat headerImageHeight = 280;

@interface HouseDynamicViewController ()<UITableViewDelegate,UITableViewDataSource,ShowImageViewDelegate,HouseListViewControllerDelegate,UIAlertViewDelegate>
{
    NSMutableArray *imageList;
    
}
@property (nonatomic,strong) ShowImageView*imageV;
@property (nonatomic,strong) UITableView *tableview;
@property (nonatomic,strong) HouserInfoModel *mymodle;
@property (nonatomic,strong) NSMutableArray *dynamicList;
@property (nonatomic,strong) HouseZhexianModel *zhexianmodel;
@end

@implementation HouseDynamicViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.dynamicList = [NSMutableArray array];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [JTCSMSingle shareSingle].houseCellHeight = 138;
    [JTCSMSingle shareSingle].navigationC = self.navigationController;
    [JTCSMSingle shareSingle].thevc = self;
    [self setTableview];
    // Do any additional setup after loading the view.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:NO];
     [df isRemoveTarbar:NO];
    if (self.ischoice) {
//        [self rebackRequestWithHouse_id:[JTCSMSingle shareSingle].choicemodel];
    }else{
        [self getRequest_one];
    }
}

- (void)setTableview{
    _tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KCONTROLLERWith, KSCREENHeight-Height_TabBar) style:UITableViewStylePlain];
    _tableview.delegate = self;
    _tableview.dataSource = self;
    _tableview.bounces = NO;
    [_tableview setHidden:YES];
    _tableview.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableview.backgroundColor = [UIColor whiteColor];
    _tableview.estimatedRowHeight = 0;
    _tableview.estimatedSectionHeaderHeight = 0;
    _tableview.estimatedSectionFooterHeight = 0;
    [self.view addSubview:_tableview];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 5;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellid = [NSString stringWithFormat:@"%ld-%ld",(long)indexPath.section,(long)indexPath.row];
    if (indexPath.row ==0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell == nil) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
            
            _imageV = [[ShowImageView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWidth, headerImageHeight)];
            [cell.contentView addSubview:_imageV];
            
        }
       
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row ==1) {
        houseInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell==nil) {
            cell = [[houseInfoTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
         [df addTapGestureRecognizerWithTarget:self action:@selector(touchBottom) andaddView:cell.bottomview];
        [cell setValueWithModel:_mymodle];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    if (indexPath.row ==2) {
        housekeeperTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell==nil) {
            cell = [[housekeeperTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        [df addTapGestureRecognizerWithTarget:self action:@selector(callphone) andaddView:cell.phoneImageV];
        [cell setvalueWithModel:_mymodle];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    if (indexPath.row ==3) {
        housedynamicTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell==nil) {
            cell = [[housedynamicTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell layoutTargetViewWithnumber:_dynamicList];
        return cell;
    }
    if (indexPath.row ==4) {
        housecountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellid];
        if (cell==nil) {
            cell = [[housecountTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell setvalueWithModel:_mymodle];
        [cell setzhexianWithmodel:self.zhexianmodel];
        [df addTapGestureRecognizerWithTarget:self action:@selector(isShowdaikanLine:) andaddView:cell.onelabel];
        [df addTapGestureRecognizerWithTarget:self action:@selector(isShowshoucangLine:) andaddView:cell.twolabel];
        [df addTapGestureRecognizerWithTarget:self action:@selector(isShowtouchLine:) andaddView:cell.threelabel];
        
        [df addTapGestureRecognizerWithTarget:self action:@selector(gotoHouseTakeVC:) andaddView:cell.daikanLabel];
        [df addTapGestureRecognizerWithTarget:self action:@selector(gotoHouseTakeVC:) andaddView:cell.shoucangLabel];
        [df addTapGestureRecognizerWithTarget:self action:@selector(gotoHouseTakeVC:) andaddView:cell.touchLabel];
        return cell;
    }
    
    return nil;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KSCREENWidth, 40)];
    view.backgroundColor = [UIColor whiteColor];
    NSString *title = @"切换房源";
    CGRect rect = [df labelHeightWithContent:title width:1000 font:15];
    CGFloat imagesize = 7;
    CGFloat labelX = (KSCREENWidth-rect.size.width-imagesize-6)/2;
    UILabel *label = [UILabel initWithFrame:CGRectMake(labelX, 0, rect.size.width, view.frame.size.height) text:title font:15 textAlignment:NSTextAlignmentLeft texcolor:RGBACOLOR(61, 61,61, 1) backgroundColor:nil];
    [view addSubview:label];
    UIImageView *imagev = [[UIImageView alloc]initWithFrame:CGRectMake(KVIEW_X(label)+6, (view.frame.size.height-imagesize)/2, imagesize/2*3, imagesize)];
    imagev.image = [UIImage imageNamed:@"xiasanjiao.png"];
    [view addSubview:imagev];
    view.userInteractionEnabled=YES;label.userInteractionEnabled=YES;imagev.userInteractionEnabled=YES;
    [df addTapGestureRecognizerWithTarget:self action:@selector(gotoHouseList) andaddView:view];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return headerImageHeight;
    }
    if (indexPath.row == 1) {
        return [JTCSMSingle shareSingle].houseCellHeight;
    }
    if (indexPath.row ==2) {
        return 86;
    }
    if (indexPath.row ==3) {
        return [housedynamicTableViewCell whc_CellHeightForIndexPath:indexPath tableView:tableView];
    }
    if (indexPath.row ==4) {
        return [housecountTableViewCell whc_CellHeightForIndexPath:indexPath tableView:tableView];
    }
    return 0;
}

#pragma mark 页面展开收起
- (void)touchBottom{
    if ([JTCSMSingle shareSingle].houseCellHeight == 278) {
        [JTCSMSingle shareSingle].houseCellHeight = 138;
    }else{
        [JTCSMSingle shareSingle].houseCellHeight = 278;
    }
    [self.tableview reloadData];
}
#pragma mark ShowImageViewDelegate
-(void)touchImageView:(NSInteger)num{
    
}
#pragma mark 拨打电话
- (void)callphone{
    NSMutableString* str=[[NSMutableString alloc] initWithFormat:@"tel:%@",(_mymodle.hmbPhone==nil||[_mymodle.hmbPhone isEqualToString:@""]?@"400-622-6266":_mymodle.hmbPhone)];
    
    UIWebView * callWebview = [[UIWebView alloc] init];
    
    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    
    [self.view addSubview:callWebview];
}
#pragma mark 控制折线显隐

- (void)isShowdaikanLine:(UITapGestureRecognizer *)tap{
    NSLog(@"带看人数");
    housecountTableViewCell *cell = (housecountTableViewCell *)[tap view].superview.superview;
    UIView *view = [cell.contentView viewWithTag:5000];
    
    if (cell.zhexianview.daikan_show) {
        [cell.zhexianview.daikanlineChartLayer removeFromSuperlayer];
        view.backgroundColor = RGBACOLOR(221, 221, 221, 1);
    }else{
        [cell.zhexianview dravLineWithType:DAIKAN_TYPE datasource:self.zhexianmodel.takeCount.mutableCopy];
        view.backgroundColor = RGBACOLOR(15,166,232,1);
    }
     cell.zhexianview.daikan_show = !cell.zhexianview.daikan_show;
}
- (void)isShowshoucangLine:(UITapGestureRecognizer *)tap{
    NSLog(@"收藏人数");
    housecountTableViewCell *cell = (housecountTableViewCell *)[tap view].superview.superview;
     UIView *view = [cell.contentView viewWithTag:5001];
    if (cell.zhexianview.shoucang_show) {
        [cell.zhexianview.shoucanglineChartLayer removeFromSuperlayer];
        view.backgroundColor = RGBACOLOR(221, 221, 221, 1);
    }else{
        [cell.zhexianview dravLineWithType:SHOCANG_TYPE datasource:self.zhexianmodel.collectCount.mutableCopy];
        view.backgroundColor = RGBACOLOR(255, 135, 38, 1);
    }
    cell.zhexianview.shoucang_show = !cell.zhexianview.shoucang_show;
    
}
- (void)isShowtouchLine:(UITapGestureRecognizer *)tap{
    NSLog(@"点击人数");
    housecountTableViewCell *cell = (housecountTableViewCell *)[tap view].superview.superview;
     UIView *view = [cell.contentView viewWithTag:5002];
    if (cell.zhexianview.touch_show) {
        [cell.zhexianview.touchlineChartLayer removeFromSuperlayer];
        view.backgroundColor = RGBACOLOR(221, 221, 221, 1);
    }else{
        [cell.zhexianview dravLineWithType:TOUCH_TYPE datasource:self.zhexianmodel.clickCount.mutableCopy];
        view.backgroundColor = RGBACOLOR(241, 124, 202, 1);
    }
    cell.zhexianview.touch_show = !cell.zhexianview.touch_show;
}
#pragma mark 点击人数查看详情
- (void)gotoHouseTakeVC:(UITapGestureRecognizer *)tap{
    UILabel *touchLabel = (UILabel *)[tap view];
    NSInteger type = touchLabel.tag-300;
    NSLog(@"---%ld",(long)type);
    NSArray *numberArray = @[_mymodle.takeCount,_mymodle.collectCount,_mymodle.clickCount];
    if ([numberArray[type-1] integerValue] <= 0) {
        return;
    }
    HouseTakeViewController *vc = [[HouseTakeViewController alloc]init];
    vc.nameTitle = type==1?@"带看详情":(type ==2?@"收藏详情":@"点击详情");
    vc.theType = [NSString stringWithFormat:@"%ld",(long)type];
    vc.houseSourceId = _mymodle.Newid;
    [self.navigationController pushViewController:vc animated:YES];
    
    
    
}
#pragma mark 切换房源
- (void)gotoHouseList{
    HouseListViewController *vc = [[HouseListViewController alloc]init];
    vc.nameTitle = @"房源动态";
    vc.delegate = self;
    vc.house_id = self.mymodle.Newid;
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark 房源列表 houseListViewControllerDelegate
- (void)rebackRequestWithHouse_id:(HouserInfoModel *)house_model{
    [JTCSMSingle shareSingle].choicemodel = house_model;
    self.ischoice = YES;
    [self.dynamicList removeAllObjects];
    self.dynamicList = nil;
    self.dynamicList = [NSMutableArray array];
    __block HouseDynamicViewController *myself = self;
    
    [[LoadData object] loadDataPostAsynchronousWithURL:@"hkapp/saleHouse/getSaleHouseDetail" withDict:@{@"phone":KHkTellString,@"token":KTokenString,@"isSupper":@"",@"houseSourceId":KTurnToString(house_model.Newid),@"code":KTurnToString(house_model.code)} superView:self.view Block:^(id  _Nullable responseObject) {
        if (responseObject ==nil) {
            //            [df showAlertWithTitle:@"提示" message:@"请求失败" btn1:@"知道了" btn2:nil btn3:nil delegate:nil tag:0];
            return ;
        }
         [JTCSMSingle shareSingle].houseCellHeight = 138;
        myself.mymodle = [HouserInfoModel new];
        [myself.mymodle setValuesForKeysWithDictionary:[KResponseData objectForKey:@"house"]];
        NSArray *list =[KResponseData objectForKey:@"dynamicList"];
        for (int i = 0; i<list.count; i++) {
            HouserInfoModel *model = [HouserInfoModel  new];
            [model setValuesForKeysWithDictionary:list[i]];
            [myself.dynamicList addObject:model];
        }
        [self->imageList removeAllObjects];
        self->imageList = [NSMutableArray arrayWithArray:[KResponseData objectForKey:@"imgList"]];
        [myself.imageV viewDidLoadWithImageArray:self->imageList];
        if (myself.mymodle.Newid  !=nil) {
            [self zhexiantuRequest];
        }else{
            [self.tableview reloadData];
        }
        
    }];
}
#pragma mark 网络请求

- (void)getRequest_one{
    [self.dynamicList removeAllObjects];
    self.dynamicList = nil;
    self.dynamicList = [NSMutableArray array];
    __block HouseDynamicViewController *myself = self;
    [[LoadData object] loadDataPostAsynchronousWithURL:@"hkapp/saleHouse/getSaleHouseDetail" withDict:@{@"phone":KHkTellString,@"token":KTokenString,@"isSupper":@""} superView:nil Block:^(id  _Nullable responseObject) {
        if (![[responseObject allKeys] containsObject:@"data"] ) {
            [df getImageView:myself.view isbigImaga:@"datanull.jpg" Target:myself action:@selector(gotoSellHouse)];
            [myself.tableview setHidden:YES];
            return ;
        }else{
            [df removeImageView:self.view];
            [myself.tableview setHidden:NO];
        }
        myself.mymodle = [HouserInfoModel new];
        if ([KResponseData objectForKey:@"house"] !=nil) {
            [myself.mymodle setValuesForKeysWithDictionary:[KResponseData objectForKey:@"house"]];
        }
        
        NSArray *list =[KResponseData objectForKey:@"dynamicList"];
        if (list !=nil) {
            for (int i = 0; i<list.count; i++) {
                HouserInfoModel *model = [HouserInfoModel new];
                [model setValuesForKeysWithDictionary:list[i]];
                [myself.dynamicList addObject:model];
            }
        }
        
        if ([KResponseData objectForKey:@"imgList"]!=nil) {
            [self->imageList removeAllObjects];
            self->imageList = [NSMutableArray arrayWithArray:[KResponseData objectForKey:@"imgList"]];
            [myself.imageV viewDidLoadWithImageArray:self->imageList];
        }
        if (myself.mymodle.Newid !=nil) {
            [self zhexiantuRequest];
        }else{
            [self.tableview reloadData];
        }
        
        
    }];
}
#pragma mark 折线图数据请求
- (void)zhexiantuRequest{
    __block HouseDynamicViewController *myself = self;
    [[LoadData object] loadDataPostAsynchronousWithURL:@"hkapp/house/getChartDataList" withDict:@{@"houseSourceId":self.mymodle.Newid ,@"token":KTokenString,@"phone":KHkTellString} superView:nil Block:^(id  _Nullable responseObject) {
         myself.zhexianmodel = [HouseZhexianModel whc_ModelWithJson:KResponseData];
        if([myself.mymodle.saleStatus integerValue]>=6){
            myself.zhexianmodel.isShow = NO;
        }else{
            myself.zhexianmodel.isShow = YES;
        }
       
         [self.tableview reloadData];
    }];
}

#pragma mark 跳转web页
- (void)gotoSellHouse{
    WebViewController *vc = [[WebViewController alloc]init];
    vc.urlStr = KWEBURLHEADER(@"QuickSeller.html");
    vc.phone = KHkTellString;
    [self.navigationController pushViewController:vc animated:YES];
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/


@end
